package com.xzj.admin.resource.sync.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xzj.admin.resource.sync.domain.dict.DictItemTree;
import com.xzj.admin.resource.sync.domain.vo.SelectOption;
import com.xzj.admin.resource.sync.domain.vo.SelectPortOption;
import com.xzj.admin.resource.sync.mapper.DictMapper;
import com.xzj.common.core.exception.ServiceException;
import com.xzj.common.id.gen.DefaultIdHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xzj.common.datasource.domain.PageReq;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.common.datasource.utils.XzjPageUtil;
import com.xzj.admin.resource.sync.domain.entity.ResScriptManage;
import com.xzj.admin.resource.sync.mapper.ResScriptManageMapper;
import com.xzj.admin.resource.sync.service.ResScriptManageService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: xzj-admin
 * @ClassName: ResScriptManageServiceImpl
 * @description: 资源处理脚本管理;(res_script_manage)表服务实现类
 * @author: XiaZunJun
 * @create: 2024-10-24
 */
@Service
public class ResScriptManageServiceImpl implements ResScriptManageService{
    @Resource
    private ResScriptManageMapper resScriptManageMapper;

    @Resource
    private DictMapper dictMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param scriptId 主键
     * @return 实例对象
     */
    public ResScriptManage queryById(String scriptId){
        return resScriptManageMapper.queryById(scriptId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    public PageResp<ResScriptManage> pageQuery(PageReq<ResScriptManage> pageReq){
        return XzjPageUtil.queryPage(pageReq, resScriptManage -> {
            if(null != resScriptManage.getCloudOptions()){
                resScriptManage.setCloudId(resScriptManage.getCloudOptions().get(0));
                resScriptManage.setCloudType(resScriptManage.getCloudOptions().get(1));
            }
            List<ResScriptManage> resScriptManages = resScriptManageMapper.queryAllByLimit(resScriptManage);
            resScriptManages.forEach(ResScriptManage::buildCloudOptions);
            return resScriptManages;
        });
    }

    /**
     * 新增数据
     *
     * @param resScriptManage 实例对象
     * @return 实例对象
     */
    public ResScriptManage insert(ResScriptManage resScriptManage){
        if(StrUtil.isNotEmpty(resScriptManage.getTypeUse()) || (StrUtil.isAllNotEmpty(resScriptManage.getScriptType(),resScriptManage.getScriptUse()))){
            resScriptManage.setScriptId(DefaultIdHelper.nextIdStr());
            String[] split = resScriptManage.getTypeUse().split(":");
            resScriptManage.setScriptType(split[0]);
            resScriptManage.setScriptUse(split[1]);
            List<String> cloud = resScriptManage.getCloudOptions();
            if(null != cloud && !cloud.isEmpty()){
                resScriptManage.setCloudId(cloud.get(0));
                resScriptManage.setCloudType(cloud.get(1));
            }
            resScriptManageMapper.insert(resScriptManage);
            return resScriptManage;
        } else {
          throw new ServiceException("100010","缺少必要参数");
        }
    }

    /**
     * 更新数据
     *
     * @param resScriptManage 实例对象
     * @return 实例对象
     */
    public ResScriptManage update(ResScriptManage resScriptManage){
        List<String> cloud = resScriptManage.getCloudOptions();
        if(null != cloud && !cloud.isEmpty()){
            resScriptManage.setCloudId(cloud.get(0));
            resScriptManage.setCloudType(cloud.get(1));
        }
        resScriptManageMapper.update(resScriptManage);
        return resScriptManage;
    }

    /**
     * 通过主键删除数据
     *
     * @param scriptId 主键
     * @return 是否成功
     */
    public boolean deleteById(String scriptId){
        int total = resScriptManageMapper.deleteById(scriptId);
        return total > 0;
    }

    /**
     * 获取字典树
     * @return
     */
    @Override
    public List<DictItemTree> getDictTree() {
        // 获取脚本类型字段数据
        List<DictItemTree> typeList = dictMapper.queryItemByType("1006");
        // 组装数据
        for (DictItemTree type : typeList) {
            // 获取脚本用途字段数据
            List<DictItemTree> useList = dictMapper.queryItemByType("1007");
            for (DictItemTree use : useList) {
               use.setParentCode(type.getDictCode());
               use.setDictCode(type.getDictCode()+":"+use.getDictCode());
            }
            type.setChildren(useList);
        }
        return typeList;
    }

    @Override
    public List<DictItemTree> cloudTypeCascade() {
        // 获取云资源ID字段数据
        List<DictItemTree> idList = dictMapper.queryItemByType("1001");
        for (DictItemTree id : idList) {
            // 获取云资源类型字段数据
            List<DictItemTree> typeList = dictMapper.queryItemByType(id.getDictCode());
            typeList.forEach(item -> {
                item.setParentCode(id.getDictCode());
                item.setDictCode(id.getDictCode()+":"+item.getDictCode());
            });
            id.setChildren(typeList);
        }
        return idList;
    }

    @Override
    public Map<String, List<SelectPortOption>> queryAllByCloud(String cloudOption) {
        Map<String, Map<String, SelectPortOption>> selectMap = new HashMap<>();
        List<ResScriptManage> resScriptManages = resScriptManageMapper.queryAllByCloud(cloudOption);
        resScriptManages.forEach(script -> {
            Map<String, SelectPortOption> selectOptionMap;
            if(selectMap.containsKey(script.getScriptUse())){
                selectOptionMap = selectMap.get(script.getScriptUse());
            }else{
                selectOptionMap = new HashMap<>();
                selectMap.put(script.getScriptUse(), selectOptionMap);
            }
            SelectPortOption selectOptions;
            if(selectOptionMap.containsKey(script.getScriptType())){
                selectOptions = selectOptionMap.get(script.getScriptType());
            }else{
                selectOptions = new SelectPortOption();
                selectOptions.setLabel(script.getScriptType());
                selectOptionMap.put(script.getScriptType(), selectOptions);
            }
            SelectOption selectOption = new SelectOption();
            selectOption.setLabel(script.getScriptName());
            selectOption.setValue(script.getScriptId());
            selectOptions.getOptions().add(selectOption);
        });
        Map<String, List<SelectPortOption>> selectPortMap = new HashMap<>();
        selectMap.forEach((key, value) -> {
            List<SelectPortOption> selectPortOptions = new ArrayList<>();
            for(String valueKey : value.keySet()){
                selectPortOptions.add(value.get(valueKey));
            }
            selectPortMap.put(key, selectPortOptions);
        });
        return selectPortMap;
    }
}